package com.example.demo;

import com.example.demo.Entity.User;
import com.example.demo.Entity.UserList;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.util.Arrays;

@RestController
public class index{

    @RequestMapping("/hello")
    public String hello(){
        return "Hello World";
    }

    @RequestMapping("/simpleParams")
    public String simpleParam(HttpServletRequest request) {
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        return name + ":" + age;
    }

    @RequestMapping("/simpleEntity")
    public User simplePojo(User user){
        return user;
    }

    @RequestMapping("/complexEntity")
    public User ComplexPojo(@RequestBody User user){
        return user;
    }

    @RequestMapping("/arrayParams")
    public String ArrayParams(String[]hobbys){
        return Arrays.toString(hobbys);
    }

    @RequestMapping("/list")
    public UserList List(@RequestBody UserList list){
        return list;
    }

    @RequestMapping("/dateParam")
    public String dateParam(@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") LocalDateTime date){
        String arrays[]=date.toString().split("T");
        String result=arrays[0];

        for(int i=1;i<arrays.length;i++){
            result+=" "+arrays[i];
        }
        return result;
    }

    @RequestMapping("/JSONparams")
    public User JSONparams(@RequestBody User user){
        System.out.println(user);
        return user;
    }

    @RequestMapping("/path/{id}/{name}")
    public String pathParams(@PathVariable Integer id,@PathVariable String name){
        System.out.println(id+" "+name);
        return "Ok";
    }

}
